<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="资讯标题" prop="infoTitle">
      <el-input v-model="dataForm.infoTitle" placeholder="资讯标题"></el-input>
    </el-form-item>
      <el-form-item label="组织名称" prop="deptId">
        <el-select v-model="dataForm.deptId" placeholder="请选择组织" :disabled="deptShow" @change="selectDept">
          <el-option
            v-for="item in deptValue"
            :key="item.deptId"
            :label="item.deptNm"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模块名称" prop="moduleId">
        <el-select v-model="dataForm.moduleId" placeholder="请选择模块"  @change="selectModule">
          <el-option
            v-for="item in moduleValue"
            :key="item.moduleId"
            :label="item.moduleName"
            :value="item.moduleId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资讯内容" prop="infoContent">
        <vue-neditor-wrap v-model="dataForm.infoContent" :config="myConfig" @ready="ready"  :destroy="false"></vue-neditor-wrap>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import VueNeditorWrap from 'vue-neditor-wrap'
  export default {
    components: {
      VueNeditorWrap
    },
    data () {
      return {
        deptValue: [],
        deptShow: false,
        moduleValue: [],
        myConfig: {
          UEDITOR_HOME_URL: '/static/NEditor/',
          autoHeightEnabled: false,
          initialFrameHeight: 240,
          initialFrameWidth: '100%',
          enableAutoSave: false
        },
        visible: false,
        dataForm: {
          infoId: 0,
          infoTitle: '',
          infoContent: '',
          status: '',
          createTime: '',
          updateTime: '',
          createId: '',
          updateId: '',
          moduleId: '',
          tenantId: '',
          moduleName: '',
          deptId: '',
          deptName: ''
        },
        dataRule: {
          infoTitle: [
            { required: true, message: '资讯标题不能为空', trigger: 'blur' }
          ],
          infoContent: [
            { required: true, message: '资讯内容不能为空', trigger: 'blur' }
          ],
          moduleId: [
            { required: true, message: '请选择配置模块', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '请选择组织名称', trigger: 'blur' }
          ]
        }
      }
    },

    methods: {
      init (id) {
        this.dataForm.infoId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.$http({
            url: this.$http.adornUrl('/info/moduleconfigtenant/listFor'),
            method: 'get',
            params: this.$http.adornParams({
              'moduleType': '2'
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log(data.list)
              this.moduleValue = data.list
            }
          })
          this.$http({
            url: this.$http.adornUrl('/dept/dept/listFor'),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log(data.list)
              this.deptValue = data.list
            }
          })
          if (this.dataForm.infoId) {
            this.$http({
              url: this.$http.adornUrl(`/info/infomodule/info/${this.dataForm.infoId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.infoTitle = data.infoModule.infoTitle
                this.dataForm.infoContent = data.infoModule.infoContent
                this.dataForm.status = data.infoModule.status
                this.dataForm.createTime = data.infoModule.createTime
                this.dataForm.updateTime = data.infoModule.updateTime
                this.dataForm.createId = data.infoModule.createId
                this.dataForm.updateId = data.infoModule.updateId
                this.dataForm.moduleId = data.infoModule.moduleId
                this.dataForm.moduleName = data.infoModule.moduleName
                this.dataForm.tenantId = data.infoModule.tenantId
                this.dataForm.deptId = data.infoModule.deptId
                this.dataForm.deptName = data.infoModule.deptName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/info/infomodule/${!this.dataForm.infoId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'infoId': this.dataForm.infoId || undefined,
                'infoTitle': this.dataForm.infoTitle,
                'infoContent': this.dataForm.infoContent,
                'moduleId': this.dataForm.moduleId,
                'moduleName': this.dataForm.moduleName,
                'deptId': this.dataForm.deptId,
                'deptName': this.dataForm.deptName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      selectDept (deptId) {
        let obj = {}
        obj = this.deptValue.find((item) => {
          return item.deptId === deptId
        })
        console.log(obj.deptNm)
        this.dataForm.deptName = obj.deptNm
      },
      selectModule (moduleId) {
        let obj = {}
        obj = this.moduleValue.find((item) => {
          return item.moduleId === moduleId
        })
        this.dataForm.moduleName = obj.moduleName
      },
      ready (editorInstance) {
        console.log(`编辑器实例${editorInstance.key}: `, editorInstance)
      }
    }
  }
</script>
